package net.skyedu.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.skyedu.bean.StuFee;
import net.skyedu.bean.StuFeeSecond;
import net.skyedu.bean.Sum;
import net.skyedu.dao.AjaxDAO;

public class PaginationSecond extends HttpServlet{
	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
              doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		  response.setCharacterEncoding("utf-8");
		  	
		  	int currentpage = Integer.parseInt(request.getParameter("currentPage"));
	  		int pagesize = Integer.parseInt(request.getParameter("pageSize"));
	  		int terId = Integer.parseInt(request.getParameter("terId"));
	  		int schId = Integer.parseInt(request.getParameter("schId"));
	  		int subId = Integer.parseInt(request.getParameter("subId"));
	  		int graId = Integer.parseInt(request.getParameter("graId"));
//	  		int feeClass = Integer.parseInt(request.getParameter("feeClass"));
	  		int surplusfee = Integer.parseInt(request.getParameter("surplusfee"));
	  		String teaName = request.getParameter("teaName");
	  		String stuName = request.getParameter("stuName");
	  		String startTime = request.getParameter("startTime");
	  		String endTime = request.getParameter("endTime");
	  		StringBuffer sb = new StringBuffer();
	  		StringBuffer sb1 = new StringBuffer();
	  		if(startTime != null && !"".equals(startTime)){
	  			startTime = sb.append(startTime).append(" 00:00:00").toString();
	  		}
	  		if(endTime != null && !"".equals(endTime)){
	  			endTime = sb1.append(endTime).append(" 23:59:59").toString();
	  		}
			if(teaName != null && !"".equals(teaName)){
				teaName = URLDecoder.decode(teaName,"UTF-8");
			}
			if(stuName != null && !"".equals(stuName)){
				stuName = URLDecoder.decode(stuName,"UTF-8");
			}
         
	  		int startIndex = 0;
	  		try{
        	  startIndex = (currentpage - 1) * pagesize;
        	  
        	  AjaxDAO ad = new AjaxDAO();
//		      StringBuffer jsonAttr = new StringBuffer("{\"pageCount\":\""+pageCount+"\",\"pageData\":[");
		      StringBuffer jsonAttr = new StringBuffer("[");
		      int pageCount = 0;
		      //学生查询
        		  List<StuFeeSecond> stulist = ad.showlistSecond(terId, schId, subId, graId, teaName, stuName, startTime, endTime, startIndex, pagesize, 1,surplusfee);
        		  pageCount = ad.showlistSecond(terId, schId, subId, graId, teaName, stuName, startTime, endTime, startIndex, pagesize, 0,surplusfee).size();
        		  if(pageCount != 0){
        			
        			  double sumShouldFee=0.0d;
        			  double sumActualFee=0.0d;
        			  double sumUnAcceptFee=0.0d;
        			  double sumsurplusFeeBefore=0.0d;
        			  double sumsurplusFee=0.0d;
        			  double sumCarryOver=0.0d;
        			  double sumRollOut=0.0d;
        			  int sumclaNum=0;
        			  int sumAlreadyClaNum=0;
//        			  double sumSurplusClaNum=0;
        			  double sumotherCheaper=0.0d;
        			  double sumalreadyfee=0.0d;
        			  double sumbooksum=0.0d;
        			  double refundsum=0.0d;
//        			  double sumunAcceptFee=0.0d;
//        			  Sum sum = ad.getSum(terId, schId, subId, graId, teaName, stuName, startTime, endTime);
    			      for(int i = 0; i < stulist.size(); i++){
    			    	 
    			    	  StuFeeSecond stu = stulist.get(i);
    			    	  int num = ad.countYiNum(terId,stu.getStuId(),stu.getSubName().trim(),stu.getClassType(),stu.getCourseid());//得到已上课次
//    			    	  int sumnum=ad.countsum(terId, stu.getClaId());
    			    	  int tempnum=i+1;
    			    	  if(tempnum<stulist.size())
    			    	  {
    			    	  System.out.println(stulist.get(tempnum).getStuId());
    			    	  System.out.println(stu.getStuId());
    			    	  }
    			    	  if(tempnum<stulist.size()&&stulist.get(tempnum).getStuId().equals(stu.getStuId()))
    			    	  {
    			    		  stulist.get(tempnum).setBalance(0.0);
    			    	  }
    			    	  double surplusFeeBefore=stu.getBalance();
//    			    	  double surplusFeeBefore=ad.surplusFeeBefore(terId, stu.getStuId());//往期结余
    			    	  stu.setSurplusbefore(surplusFeeBefore); 
    			    	  stu.setAlreadyClaNum(num);//已上课次
//    			    	  if(stu.getStuName().equals("谢东桐"))
//    			    	  {
//    			    		  System.out.println(stu.getClaNum());
//    			    		  
//    			    	  }
    			    	  
    			    	  double  otherCheaper=stu.getOtherCheaper();
    			  
//    			    	 double shouldfee= stu.getShouldFee();
    			    	 double alreadyfee=stu.getAlreadyClaNum()*stu.getFeeperClass()*stu.getDiscount(); //已经花费
    			    	 System.out.println(stu.getStuName()+"上课次数："+stu.getAlreadyClaNum()+"   单次课费用："+stu.getFeeperClass()+"   折扣："+stu.getDiscount());
//    			    	 if(feeClass==1&&feetemp>0)
//    			    	 {
//    			    		 Map<String,Object> tempmap=new HashMap<String,Object>();
//    			    		getJsonAttr(terId,stu,jsonAttr,tempmap);
//    			    	 }
//    			    	 if(feeclass)
//    			    	 {
//    			    		 
//    			    	 }
//    			    	if(stu.getStuId()==66)
//    			    	{
//    			    		System.out.println(stu.getShouldFee());
//    			    	}
    			    	 sumRollOut+=stu.getRollout();
    			    	 sumCarryOver+=stu.getCarryOver();
    			    	 refundsum+=stu.getRefundfee();
    			    	 sumbooksum+=stu.getBooksum();
    			    	 sumalreadyfee+=alreadyfee;   //消耗费用累加
    			    	 System.out.println("消耗费用"+sumalreadyfee);
    			    	 sumotherCheaper+=otherCheaper;
    			    	 sumsurplusFeeBefore+=surplusFeeBefore;
//    			    	  sumShouldFee+=shouldfee;//应该交费
    			    	 sumShouldFee+=stu.getShouldFee();//应该交费
    			    	  sumActualFee+=stu.getActualFee();//实际交费
    			    	  sumUnAcceptFee+=stu.getNotPay();//未缴清
    			    	  sumclaNum+=stu.getClaNum();//交费课次
    			    	  sumAlreadyClaNum+=stu.getAlreadyClaNum();//已上课次
//    			    	  sumSurplusClaNum+=stu.getSurplusClaNum();//剩余课次
    			    	  jsonAttr.append("{");
    			    	  jsonAttr.append("\"courseid\":\""+stu.getCourseid()+"\",");
    			    	  jsonAttr.append("\"stuId\":\""+stu.getStuId()+"\",");
    			    	  jsonAttr.append("\"stuName\":\""+stu.getStuName()+"\",");
    			    	  jsonAttr.append("\"terName\":\""+stu.getTerName()+"\",");
    			    	  jsonAttr.append("\"schName\":\""+stu.getSchName()+"\",");
    			    	  jsonAttr.append("\"deptid\":\""+stu.getDeptId()+"\",");
    			    	  jsonAttr.append("\"subName\":\""+stu.getSubName()+"\",");
    			    	  jsonAttr.append("\"graName\":\""+stu.getGraName()+"\",");
    			    	  jsonAttr.append("\"rollout\":\""+stu.getRollout()+"\",");
    			    	  jsonAttr.append("\"courseName\":\""+stu.getCoursename()+"\",");
    			    	  jsonAttr.append("\"shouldFee\":\""+stu.getShouldFee()+"\",");
    			    	  
    			    	  int type=0;
			    		  if("英语".equals(stu.getSubName().trim()))
			    		  {
			    			  type=1;
			    		  }
			    		  else if("数学".equals(stu.getSubName().trim()))
			    		  {
			    			  type=2;
			    		  }
			    		  else if("语文".equals(stu.getSubName().trim()))
			    		  {
			    			  type=3;
			    		  }
			    		  else 
			    		  {
			    			  type=4;
			    		  }
    			    	  
    			    	  //modify By jinmingming
    			    	  if(stu.getActualFee() != 0){
    			    		  
    			    		  System.out.println(stu.getStuId()+","+stu.getSubjectId()+","+stu.getGradeId()+","+terId);
    			    		  jsonAttr.append("\"actualFee\":\"<a href='javascript:void(0);' " +
    	    			    		  "onclick='openWinActualFee("+stu.getStuId()+","+stu.getSubjectId()+","+stu.getGradeId()+","+terId+","+stu.getCourseid()+");'><font color='red' style='font-weight:bold;'>"+stu.getActualFee()+"</font></a>\",");
    	    			   
    			    	  }else{
    			    		  jsonAttr.append("\"actualFee\":\"0\",");
    			    	  }
    			    	 
    			    	  
    			    	  jsonAttr.append("\"stuid\":\""+stu.getStuId()+"\",");
    			    	  jsonAttr.append("\"carryOver\":\""+stu.getCarryOver()+"\",");
    			    	  jsonAttr.append("\"discount\":\""+stu.getDiscount()+"\",");
    			    	  jsonAttr.append("\"payTime\":\""+stu.getPayTime()+"\",");
    			    	  jsonAttr.append("\"refundFee\":\""+stu.getRefundfee()+"\",");
    			    	  jsonAttr.append("\"claNum\":\""+stu.getClaNum()+"\",");
    			    	  jsonAttr.append("\"feeperClass\":\""+stu.getFeeperClass()+"\",");
    			    	  jsonAttr.append("\"booksum\":\""+stu.getBooksum()+"\",");
    			    	  
    			    	  //    			    	  jsonAttr.append("\"surplusbefore\":\""+stu.getSurplusbefore()+"\",");
    			    	  
    			    	  //jsonAttr.append("\"alreadyFee\":\""+alreadyfee+"\",");
    			    	  
			    		  
			    		  //modify By jinmingming
    			    	  if(alreadyfee!=0.0d)
    			    	  {
    			    		  jsonAttr.append("\"alreadyFee\":\"<a href='javascript:void(0);' " +
    			    	   		  "onclick='openWinAlreadyFee("+terId+","+stu.getStuId()+","+type+","+stu.getGradeId()+","+stu.getCourseid()+");'><font color='red' style='font-weight:bold;'>"+alreadyfee+"</font></a>\",");
    			    	  }
    			    	  else
    			    	  {
    			    		  jsonAttr.append("\"alreadyFee\":\"<font color='red' style='font-weight:bold;'>"+alreadyfee+"</font>\",");
        			    	 
    			    	  }
    	    			  
    			    	 
    			    	  
    			    	  double temp=stu.getActualFee()+stu.getCarryOver()+stu.getOtherCheaper()+stu.getSurplusbefore()-stu.getRefundfee()-alreadyfee-stu.getRollout();
    			    	  sumsurplusFee+=temp;
    			    	  jsonAttr.append("\"surplusFee\":\""+temp+"\",");
    			    	  jsonAttr.append("\"unPayFee\":\""+stu.getNotPay()+"\",");
//    			    	  System.out.println(stu.getStuId()+"----"+stu.getClaId());
    			    	
    			    	  if(stu.getSurplusbefore()!=0)
    			    	  {
    			    		  jsonAttr.append("\"surplusbefore\":\"<a href='javascript:void(0);' " +
    	    			    		  "onclick='openWinSurplusBalance("+terId+","+stu.getStuId()+","+type+");'><font color='red' style='font-weight:bold;'>"+stu.getSurplusbefore()+"</font></a>\",");
    	    			    	
    			    	  }
    			    	  else
    			    	  {
    			    		  jsonAttr.append("\"surplusbefore\":\"0\",");
    			    	  }
    			    	  if(otherCheaper!=0)
    			    	  {
    			    		  
    			    		  jsonAttr.append("\"otherCheaper\":\"<a href='javascript:void(0);' " +
    	    			    		  "onclick='openWinOtherCheaper("+terId+","+stu.getStuId()+","+type+");'><font color='red' style='font-weight:bold;'>"+otherCheaper+"</font></a>\",");
    	    			    	System.out.println("\"otherCheaper\":\"<a href='javascript:void(0);' " +
      	    			    		  "onclick='openWinOtherCheaper("+terId+","+stu.getStuId()+",'"+stu.getSubName().trim()+"');'><font color='red' style='font-weight:bold;'>"+otherCheaper+"</font></a>\","); 
    			    	  }
    			    	  else
    			    	  {
    			    		  jsonAttr.append("\"otherCheaper\":\"0\",");
    			    	  }
    			    	  if(num != 0){
    			    		  jsonAttr.append("\"yiNum\":\"<a href='javascript:void(0);' " +
    			    		  "onclick='openWin("+terId+","+stu.getStuId()+","+type+","+stu.getCourseid()+");'><font color='red' style='font-weight:bold;'>"+num+"</font></a>\",");
    			    	  }else{
    			    		  jsonAttr.append("\"yiNum\":\"0\",");
    			    	  }
//    			    	  double syNum = stu.getClaNum() - num;
    			    	  DecimalFormat format = new DecimalFormat("#0.0"); 
//    			    	  syNum=Double.parseDouble(format.format(syNum));
//    			    	  sumSurplusClaNum+=syNum;
//    			    	  jsonAttr.append("\"syNum\":\""+syNum+"\",");//剩余课次
    			    	  jsonAttr.append("\"recordStaff\":\""+stu.getReceiver()+"\",");
    			    	  
    			    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); //记录总数
    			    	  jsonAttr.append("}");
    			    	  
    			    	  if(i<stulist.size()-1){
    			    		  jsonAttr.append(",");
    			    	  }
    			    	  System.out.println(jsonAttr.substring(jsonAttr.toString().lastIndexOf("{")));
    			       }
    			      jsonAttr.append(",");
    			      jsonAttr.append("{");
    			      BigDecimal   b   =   new   BigDecimal(refundsum);
    			      double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
    			      jsonAttr.append("\"refundsum\":\""+f1+"\",");
    			      jsonAttr.append("\"should\":\""+sumShouldFee+"\",");
        			  jsonAttr.append("\"actual\":\""+sumActualFee+"\",");
        			  jsonAttr.append("\"cln\":\""+sumclaNum+"\",");
        			  jsonAttr.append("\"sumYiNum\":\""+sumAlreadyClaNum+"\",");
        			  jsonAttr.append("\"sumCarryOver\":\""+sumCarryOver+"\",");
        			  jsonAttr.append("\"sumotherCheaper\":\""+sumotherCheaper+"\",");
//        			  jsonAttr.append("\"synum\":\""+sumSurplusClaNum+"\",");
        			  jsonAttr.append("\"surplusFeeBefore\":\""+sumsurplusFeeBefore+"\",");//总往期结余
        			  jsonAttr.append("\"sumalreadyfee\":\""+sumalreadyfee+"\",");//总消耗费用
        			  jsonAttr.append("\"sumsurplusFee\":\""+sumsurplusFee+"\",");//总剩余费用
        			  jsonAttr.append("\"sumbooksum\":\""+sumbooksum+"\",");//总剩余费用
        			  jsonAttr.append("\"sumRollOut\":\""+sumRollOut+"\"");//转出费用
        			  
        			  
        			  jsonAttr.append("}");
        			  System.out.println(jsonAttr.substring(jsonAttr.toString().lastIndexOf("{")));
//        			  System.out.println(jsonAttr.toString().lastIndexOf("{", jsonAttr.toString().lastIndexOf("{")));
    			      
        	  }else{
		    	  jsonAttr.append("{");
		    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); 
		    	  jsonAttr.append("}");
		      }
		      
		      jsonAttr.append("]");
//		      System.out.println("JSON:---"+jsonAttr);
		      PrintWriter out = response.getWriter();
		      out.print(jsonAttr);
		      out.flush();
		      out.close();
        	
          }catch(Exception e){
        	  //写到日志表中
        	  e.printStackTrace();
          }
       
		
		
		
		
	
	}
	
//	public StringBuffer getJsonAttr(int terId,StuFeeSecond stu,StringBuffer jsonAttr,Map<String,Object> tempmap)
//	{
//		 double sumShouldFee=0.0d;
//		  double sumActualFee=0.0d;
//		  double sumUnAcceptFee=0.0d;
//		  double sumsurplusFeeBefore=0.0d;
//		  double sumsurplusFee=0.0d;
//		  int num=stu.getAlreadyClaNum();
//		  int sumclaNum=0;
//		  int sumAlreadyClaNum=0;
//		  int sumSurplusClaNum=0;
//		  double sumalreadyfee=0.0d;
//		double shouldfee= stu.getShouldFee(stu.getClaNum());
//	    double feetemp=shouldfee-stu.getActualFee();
//	    double alreadyfee=stu.getAlreadyClaNum()*stu.getFeeperClass()*stu.getDiscount(); 
//	    sumalreadyfee+=alreadyfee;
//   	 sumsurplusFeeBefore+=stu.getBalance();;
//   	  sumShouldFee+=shouldfee;//应该交费
//   	  sumActualFee+=stu.getActualFee();//实际交费
//   	  sumUnAcceptFee+=stu.getUnAcceptFee();//未缴清
//   	  sumclaNum+=stu.getClaNum();//交费课次
//   	  sumAlreadyClaNum+=stu.getAlreadyClaNum();//已上课次
//   	  sumSurplusClaNum+=stu.getSurplusClaNum();//剩余课次
//   	tempmap.put("sumalreadyfee", sumalreadyfee);
//   	tempmap.put("sumShouldFee", sumShouldFee);
//   	tempmap.put("sumUnAcceptFee", sumUnAcceptFee);
//   	tempmap.put("sumclaNum", sumclaNum);
//   	tempmap.put("sumAlreadyClaNum", sumAlreadyClaNum);
//   	tempmap.put("sumSurplusClaNum",sumSurplusClaNum);
//   	  jsonAttr.append("{");
//   	  jsonAttr.append("\"stuId\":\""+stu.getStuId()+"\",");
//   	  jsonAttr.append("\"stuName\":\""+stu.getStuName()+"\",");
//   	  jsonAttr.append("\"terName\":\""+stu.getTerName()+"\",");
//   	  jsonAttr.append("\"schName\":\""+stu.getSchName()+"\",");
//   	  jsonAttr.append("\"subName\":\""+stu.getSubName()+"\",");
//   	  jsonAttr.append("\"graName\":\""+stu.getGraName()+"\",");
//   	  jsonAttr.append("\"shouldFee\":\""+stu.getShouldFee()+"\",");
//   	  jsonAttr.append("\"actualFee\":\""+stu.getActualFee()+"\",");
//   	  jsonAttr.append("\"discount\":\""+stu.getDiscount()+"\",");
//   	  jsonAttr.append("\"payTime\":\""+stu.getPayTime()+"\",");
//   	  jsonAttr.append("\"claNum\":\""+stu.getClaNum()+"\",");
//   	  jsonAttr.append("\"feeperClass\":\""+stu.getFeeperClass()+"\",");
//   	  jsonAttr.append("\"surplusbefore\":\""+stu.getSurplusbefore()+"\",");
//   	  
//   	  jsonAttr.append("\"alreadyFee\":\""+alreadyfee+"\",");
//   	  sumsurplusFee+=(stu.getActualFee()-alreadyfee);
//   	  jsonAttr.append("\"surplusFee\":\""+(stu.getActualFee()-alreadyfee)+"\",");
//   	  jsonAttr.append("\"unPayFee\":\""+(stu.getShouldFee()-stu.getActualFee())+"\",");
////   	  System.out.println(stu.getStuId()+"----"+stu.getClaId());
//   	  
//   	  
//   	  if(num != 0){
//   		  jsonAttr.append("\"yiNum\":\"<a href='javascript:void(0);' " +
//   		  "onclick='openWin("+terId+","+stu.getStuId()+");'><font color='red' style='font-weight:bold;'>"+num+"</font></a>\",");
//   	  }else{
//   		  jsonAttr.append("\"yiNum\":\"0\",");
//   	  }
//   	  int syNum = stu.getClaNum() - num;
//   	  jsonAttr.append("\"syNum\":\""+syNum+"\",");//剩余课次
//   	  jsonAttr.append("\"recordStaff\":\""+stu.getReceiver()+"\",");
//   	  
////   	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); //记录总数
////   	  jsonAttr.append("}");
//   	  
//		
//		return jsonAttr;
//	}
//	
	
	

	public void init() throws ServletException {}
}
